﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Runtime.Serialization;
using System.Text;

namespace WsProtocol.PublicClass
{
    public enum ObjType : int
    {
        分类 = 1,
        对象 = 2
    }

    //
    // 摘要:
    //     指定一个控件的状态，例如复选框可以是选中、未选中或设置为不确定状态。
    public enum CheckState
    {
        //
        // 摘要:
        //     该控件处于未选中状态。
        Unchecked = 0,
        //
        // 摘要:
        //     该控件处于选中状态。
        Checked = 1,
        //
        // 摘要:
        //     该控件处于不确定状态。一个不确定的控件通常具有灰色的外观。
        Indeterminate = 2
    }

    //对象和属性类的基类
    [DataContract]
    public class ProNodeBase : IDisposable
    {
        private bool availability = true;  //
        private string specialid = string.Empty;

        public string ID { get; set; }  //节点ID
        public string PID { get; set; }  //节点父ID
        public string ProName { get; set; }  //节点名称

        public string inputValue { get; set; }  //界面输入值
        public string readValue { get; set; }   //抄读返回值
        public string appendValue { get; set; }  //附加参数，如抄负荷曲线的参数
        public CheckState checkState { get; set; }  //是否选中
        public bool Availability 
        {
            get { return CheckVisible(); } 
            set
            {
                availability = value;
            }
        } //有效性
        public string SpecialID
        {
            get { return GetSpecialID(); }
            set { specialid = value; }
        }

        public bool NeedReturun { get; set; }  //是否需要返回
        public ProNodeBase ParentNode { get; set; }  //父节点
        public ObjType NodeType { get; set; } //节点类型　接口类节点 0 自定义类节点　1　对象节点为　2  对应Sysconst中的ObjType
        public virtual List<ProNodeBase> EleLst { get; set; } //子节点

        //这个属性的目的是为了应对类似698协议中的元素，读写数据时取的是其父对象，但是又需要用到当前对象
        public ProNodeBase RealProNode = null;  

        public ProNodeBase()
        {
            checkState = CheckState.Unchecked;
            Availability = true;
            NeedReturun = true;
            ID = string.Empty;
            PID = string.Empty;
            ProName = string.Empty;
            inputValue = string.Empty;
            readValue = string.Empty;
            ParentNode = null;
            EleLst = new List<ProNodeBase>();
            RealProNode = this;
        }
        

        /// <summary>
        /// 检查参数有效性
        /// </summary>
        protected virtual bool CheckVisible()
        {
            return availability; 
        }

        /// <summary>
        /// 获取特殊ID，未重载时取当前ID，重载方法通常为类名+ID，本属性为了保证对象ID不重复
        /// </summary>
        /// <returns></returns>
        protected virtual string GetSpecialID()
        {
            return ID;
        }


        /// <summary>
        /// 获取全名
        /// </summary>
        /// <returns></returns>
        public virtual string GetFullName()
        {
            return ProName;
        }

        /// <summary>
        /// 加子节点
        /// </summary>
        /// <returns></returns>
        public virtual ProNodeBase AddSonNode()
        {
            ProNodeBase sonnode = new ProNodeBase();
            sonnode.ParentNode = this;
            this.EleLst.Add(sonnode);
            return sonnode;
        }

        /// <summary>
        /// 移除子节点
        /// </summary>
        /// <param name="sonnode"></param>
        public virtual void RemoveSonNode(ProNodeBase sonnode)
        {
            if (EleLst.IndexOf(sonnode) >= 0)
                EleLst.Remove(sonnode);
            sonnode.Dispose();
        }


        public virtual void SetCheckStatus(bool acheck, bool checkparent)  //设置上下级是否选中
        {
            SetCheckStatus(acheck, checkparent, true);
        }

        public virtual void SetCheckStatus(bool acheck, bool checkparent, bool checkson)  //设置上下级是否选中
        {
            if (acheck)
            {
                this.checkState = CheckState.Checked;
                if (checkson)
                {
                    for (int i = 0; i < this.EleLst.Count; i++)
                        this.EleLst[i].SetCheckStatus(true, false);
                }

                if (checkparent)
                {
                    ProNodeBase pnode = this.ParentNode;
                    while (pnode != null)
                    {
                        pnode.checkState = CheckState.Checked;
                        pnode = pnode.ParentNode;
                    }
                }
            }
            else
            {
                this.checkState = CheckState.Unchecked;
                if (checkson)
                {
                    for (int i = 0; i < this.EleLst.Count; i++)
                        this.EleLst[i].SetCheckStatus(false, false);
                }

                if (checkparent)
                {
                    ProNodeBase pnode = this.ParentNode;
                    while (pnode != null)
                    {
                        pnode.checkState = CheckState.Unchecked;

                        for (int j = 0; j < pnode.EleLst.Count; j++)
                        {
                            if (pnode.EleLst[j].checkState == CheckState.Checked)
                            {
                                pnode.checkState = CheckState.Checked;
                                break;
                            }
                        }
                        pnode = pnode.ParentNode;
                    }

                }
            }
        }


        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                //执行基本的清理代码
                if (EleLst != null)
                    EleLst.Clear();
            }
        }



    }
}
